package com.pakazaka.controllers;

import static com.pakazaka.controllers.InventoryController.processInventory;
import static com.pakazaka.controllers.MovementController.processMovement;
import com.pakazaka.enums.PlayerStateType;
import com.pakazaka.layer.TiledLayer;
import com.pakazaka.player.Player;
import com.pakazaka.simplecrawler.Core;
import com.pakazaka.testItemsSystem.Item;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

/**
 *
 * @author Riseremi
 */
public class Controller implements KeyListener {

    public static boolean pressed = false;

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        Player player = Core.getInstance().getPlayer();
        TiledLayer layer = Core.getInstance().getLayer();
        Core core = Core.getInstance();

        core.getConsole().setIsFresh(false);

        if (!pressed) {
            processInventory(e);
        }

        //test states
        if (e.getKeyCode() == KeyEvent.VK_1) {
            player.applyEffect(Player.EffectType.THIRST, 5);
        }
        if (e.getKeyCode() == KeyEvent.VK_2) {
            player.applyEffect(Player.EffectType.DRUNKENNESS, 5);
        }
        if (e.getKeyCode() == KeyEvent.VK_3) {
            player.applyEffect(Player.EffectType.HUNGER, 5);
        }

        if (e.getKeyCode() == KeyEvent.VK_ENTER && (player.canGetItem()) && !core.getInventory().isPaint()) {
            Item item = null;

            int playerX = player.getAbsoluteBlocksX();
            int playerY = player.getAbsoluteBlocksY();

            for (int i = 0; i < core.getItemsOnMap().size(); i++) {
                int itemX = core.getItemsOnMap().get(i).getBlocksX();
                int itemY = core.getItemsOnMap().get(i).getBlocksY();

                if (itemX == playerX && itemY == playerY) {
                    item = core.getItemsOnMap().get(i);
                }
            }

            if (item != null) {
                player.addItem(item);
                core.getItemsOnMap().remove(item);
                core.getConsole().pushMessage("You've just got a " + item.getName().toLowerCase());
            }
        }

        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            if (core.getInventory().isPaintDescriptionWindow()) {
                core.getInventory().setPaintDescriptionWindow(false);
            } else if (!core.getInventory().isPaintDescriptionWindow()) {
                core.getInventory().setPaint(false);
                core.getPlayer().setState(PlayerStateType.NORMAL);
            }
            core.setWhereMessage("");
        }

        //новый инвентарь
        if (!pressed && (e.getKeyCode() == KeyEvent.VK_I) && !e.isShiftDown()
                && (core.getPlayer().getState() == PlayerStateType.NORMAL || core.getPlayer().getState() == PlayerStateType.CANNOT_MOVE)) {
            core.getPlayer().setState(PlayerStateType.IN_INVENTORY);
            core.getInventory().setPaint(true);
            pressed = true;
        }

        //дроп под ноги
        if (!pressed && e.getKeyCode() == KeyEvent.VK_D && core.getPlayer().getState() == PlayerStateType.NORMAL && !e.isShiftDown()) {
            core.setWhereMessage("Drop an item");
            System.out.println("drop mode activated");
            core.getPlayer().setState(PlayerStateType.DROP);
            core.getInventory().setPaint(true);
            pressed = true;
        }

        if (core.getPlayer().getState() == PlayerStateType.NORMAL) {
            processMovement(e);
        } else if (core.getPlayer().getState() == PlayerStateType.CANNOT_MOVE) {
            Core.getInstance().getConsole().pushMessage("You cannot move for some reason!");
        }
        player.update();

        String whatYouSee = "You see here a ";

        int playerX = player.getAbsoluteBlocksX();
        int playerY = player.getAbsoluteBlocksY();
        ArrayList<Item> itemsOnMap = core.getItemsOnMap();

        for (int i = 0; i < itemsOnMap.size(); i++) {
            int itemX = itemsOnMap.get(i).getBlocksX();
            int itemY = itemsOnMap.get(i).getBlocksY();

            if (playerX == itemX && playerY == itemY) {
                whatYouSee += itemsOnMap.get(i).getName().toLowerCase() + ", ";
            }
        }
        if (!whatYouSee.equals("You see here a ")) {
            core.getConsole().pushMessage(whatYouSee.substring(0, whatYouSee.length() - 2));
        }
        core.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        pressed = false;
    }

    public static boolean isPressed() {
        return pressed;
    }

    public static void setPressed(boolean pressed) {
        Controller.pressed = pressed;
    }
}
